using UnityEngine;
using System.Collections;

public enum BlockType {
	Dirt,
	Sand,
	Concrete,
	Gas,
	GasVisible,
	Impassable
}

public class Block : AbstractElement {
	
	public delegate void BlockEvent(Block b);
	public event BlockEvent EventDestroyed;
	
	public Texture2D textureDirt;
	public Texture2D textureSand;
	public Texture2D textureConcrete;
	public Texture2D textureImpassable;
	public Texture2D textureGrass;
	public Texture2D textureGas;
	
	public Transform prefabBlockSmash;
	public Transform prefabBlockChip;
	
	public BlockType type { get; private set; }
	public float health { get; private set; }
	public int x { get; private set; }
	public int y { get; private set; }
	public bool IsSupported { get { return _supportBlock!=null; }}
	public bool IsLadder { get { return IsDestroyed && _hasLadder; }}
	public float blockDestroyedTime { get; private set; }
	
	private GameEngine _engine;
	private Block _supportBlock;
	private MeshRenderer _renderer;
	private Transform _chip;
	private bool _hasLadder;
	
	public void ApplyDamage(float amount){
		if(amount<=0 || type==BlockType.Impassable) return;
		
		health -= amount;
		if(health<=0 || _engine.player.godMode){
			Destroy();
		} else if(_chip==null){
			_chip = Instantiate(prefabBlockSmash,transform.position+new Vector3(0,0,-0.05f),Quaternion.identity) as Transform;
			_chip.renderer.material.SetTexture("_MainTex",_renderer.material.mainTexture);	
		}
		
	}
	public void Destroy(){
		if(type==BlockType.Impassable || IsDestroyed) return;
		
		IsDestroyed = true;
		
		blockDestroyedTime = _supportBlock!=null && _supportBlock.IsDestroyed ? _supportBlock.blockDestroyedTime+Random.Range(0f,2.0f) : Time.time+Random.Range(0f,300.0f);
		
		Transform t = Instantiate(prefabBlockSmash,transform.position,Quaternion.identity) as Transform;
		t.renderer.material.SetTexture("_MainTex",_renderer.material.mainTexture);
		t.position = t.position+new Vector3(0,0,1f);
		
		health = 0;	
		if(EventDestroyed!=null) EventDestroyed(this);
	}
	
	public void AddLadder(){
		if(!IsDestroyed || _hasLadder) return;
		
		_hasLadder = true;
	}
	
	/*
	public void AddSupportBeam(){
		if(!IsDestroyed || IsSupported) return;
		
		gameObject.SetActiveRecursively(true);
		
		int i=x-2, l=x+2;
		for(; i<=l; ++i){
			Block b = _engine.GetBlockAt(i,y);
			if(b!=null){
				b._supportBlock = this;
				if(b.IsDestroyed) b.blockDestroyedTime = Time.time+_engine.player.supportTime+Random.Range(0f,2.0f);
			}
		}
		
	}
	*/
	
	public void Setup(BlockType type, int x, int y, GameEngine engine){
		this.type = type;
		
		switch(type){
			case BlockType.Dirt : 
				this.health = 80f;
				_renderer.material.SetTexture("_MainTex",y==engine.gridHeight-1 ? textureGrass : textureDirt);
				break;
			case BlockType.Sand : 
				this.health = 25f; 
				_renderer.material.SetTexture("_MainTex",textureSand);
				break;
			case BlockType.Concrete : 
				this.health = 180f; 
				_renderer.material.SetTexture("_MainTex",textureConcrete);
				break;	
			case BlockType.Gas :
				this.health = 50f; 
				_renderer.material.SetTexture("_MainTex",textureDirt);
				break;
			case BlockType.GasVisible :
				this.health = 50f; 
				_renderer.material.SetTexture("_MainTex",textureGas);
				break;
			case BlockType.Impassable :
				this.health = float.MaxValue; 
				_renderer.material.SetTexture("_MainTex",textureImpassable);
				break;
		}
		
		this.x = x;
		this.y = y;
		
		transform.position = new Vector3(_bounds.width*x,_bounds.height*y,0);
		
		_engine = engine;
		_supportBlock = null;
		_hasLadder = false;
		IsDestroyed = false;
	}
	
	protected void Awake(){
		IsDestroyed = false;
		
		_renderer = GetComponentInChildren<MeshRenderer>();
		Bounds b = _renderer.bounds;
		_bounds = new Rect(-b.extents.x,-b.extents.y,b.extents.x*2f,b.extents.y*2f);
	}
}

